package com.shanghaionstar.alarm;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.onstar.cn.common.util.PreferencesUtls;
import com.shanghaionstar.R;
import com.shanghaionstar.utils.NotificationUtil;
import com.shanghaionstar.vehicle_ownerlife.DateHelper;
import com.shanghaionstar.vehicle_ownerlife.InspectHelper;

/**
 * 车况建议体检闹钟
 * <p>
 * Created by Yonglang Hong on 2017/1/11.
 */
public class VehicleInspectionAlarm implements Alarm {

    private static final String TAG = VehicleInspectionAlarm.class.getSimpleName();

    @Override
    public void alarm(Context context) {
        cancel(context);
        AlarmHelper.setAlarm(context, AlarmActionType.VEHICLE_INSPECTION_ACTION.getAction(), InspectHelper.getInspectDate(context), 2);
        AlarmHelper.setAlarm(context, AlarmActionType.VEHICLE_INSPECTION_ACTION.getAction(), InspectHelper.getInspectDate(context), 6);
        AlarmHelper.setAlarm(context, AlarmActionType.VEHICLE_INSPECTION_ACTION.getAction(), InspectHelper.getInspectDate(context), 29);
    }

    @Override
    public void cancel(Context context) {
        AlarmHelper.cancelAlarm(context, AlarmActionType.VEHICLE_INSPECTION_ACTION.getAction());
    }

    @Override
    public void showAlarmTip(Context context, Intent intent) {
        Log.d(TAG,"showAlarmTip");
        if (AlarmActionType.VEHICLE_INSPECTION_ACTION.getAction().equalsIgnoreCase(intent.getAction())) {
            int days = Math.abs(DateHelper.getBetweenDays(InspectHelper.getInspectDate(context)));
            Log.d(TAG,"showAlarmTip" + days);
            String tipTimesKey = InspectHelper.getInspectDate(context) +"_"+ days;
            if (days == 2 || days == 6 || days == 29) {
                int times =  PreferencesUtls.getInstance(context).getInt(tipTimesKey,0);
                if (times < 3) { //最多提醒三次
                    PreferencesUtls.getInstance(context).putInt(tipTimesKey, times + 1);
                    NotificationUtil.showNotification(context,
                            context.getResources().getString(R.string.vehicle_inspection_tip),
                            String.format(context.getResources().getString(R.string.vehicle_inspection_tip_msg), days + 1),
                            "com.shanghaionstar.vehicle_ownerlife.VehicleInspectionActivity");
                }
            }else {
                PreferencesUtls.getInstance(context).putInt(tipTimesKey, 0);
            }
        }
    }
}
